var http = require('http');

var bookdata = {
    "status": 200, // 状态码200都代表完全成功 - 无任何意义随便写, 只是方便前端写判断条件
    "msg": "获取图书列表成功", // 后台返回的提示消息, 随便写, 只是方便前端直接打印提示消息
    "data": [ // 后台返回的数据
        { "id": 1, "bookname": "西游记", "author": "吴承恩", "publisher": "北京图书出版社" },
        { "id": 2, "bookname": "红楼梦", "author": "曹雪芹", "publisher": "上海图书出版社" },
        { "id": 3, "bookname": "三国演义", "author": "罗贯中", "publisher": "北京图书出版社" }
    ]
}

var addbook = {
    "status": 201, // 后台返回数据逻辑层的状态码, 201代表后台已经新增加了一个资源
    "data": {
        "author": "施大神",
        "bookname": "水浒传2",
        "id": 41,
        "publisher": "未来出版社"
    },
    "msg": "添加图书成功"
}


var srv = http.createServer(function (req, res) {
    res.setHeader("Access-Control-Allow-Origin", "*");

    res.writeHead(200, { 'Content-Type': 'application/json' });
    var data = '';
    var url = req.url;
    console.log(url)
    if (url == '/api/getbooks') {
        data = bookdata
    } else if ('/api/addbook') {
        data = addbook
    }
    res.end(JSON.stringify(data));

});


srv.listen(8080, function () {
    console.log('listening on localhost:8080');
});